<!--简单通用的分类模块-->
<template>
  <el-card class="left-card">
    <el-scrollbar class="scroll-tree">
      <el-tree
        ref="orgTree"
        :props="props"
        :load="loadNode"
        lazy
        :default-expand-all="true"
        :highlight-current="true"
        :expand-on-click-node="false"
        @node-click="handleNodeClick">
      </el-tree>
    </el-scrollbar>
  </el-card>
</template>
<script>
//引入默认的修改列表
import formUtils from '@/utils/formUtils';
export default {
  name:'fancySysCategory',
  props:{
    value: {
      type:String,
      default:''
    },
    modelName: {
      type: String,
      default:''
    },
    modelKey: {
      type: String,
      default:'category'
    },
    title:{
      type:String,
      default:'分类'
    }
  },
  data(){
    return {
      currentTreeValue:'',
      currentTreeName:'',
      defaultExpandTreeNode: [],
      props: {
        label: 'name'
      },
      RootNode: {"id":"","name":''}
    }
  },
  created() {
    this.RootNode.name = this.title;
  },
  methods:{
    loadNode(node, resolve) {
      if (node.level === 0) {
        this.getTreeData({},(data)=>{
          let tree = [this.RootNode];
          resolve(tree);
        });
      } else {
        this.getTreeData(node.data,(data)=>resolve(data));
      }
    },
    getTreeData(data,callback){
      let params = {
        fdParentId:this.currentTreeValue,
        fdModelName:this.modelName,
        fdModelKey:this.modelKey
      }
      if(formUtils.isNotEmpty(data.id)){
        params.fdParentId = data.id;
      }
      this.$fetch('/api/sys/category/sys_category_main/treeData',params).then((res) => {
        if(typeof(callback) !='undefined'&& typeof(callback) === 'function'){
          if(res.status==1){
            return callback(res.data);
          }
        }
      });
    },
    handleNodeClick(data) {
      this.currentTreeValue = data.id;
      this.currentTreeName = data.name;
      this.$emit('input', this.currentTreeValue);
      this.$emit('update:currentTreeName',this.currentTreeName); 
      this.$emit('select',data,data.id,data.name);
    }
  }
}
</script>
<style>

</style>